﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using DrawTools.Properties;

namespace DrawTools.Controls.ColorSelector
{
  [ToolboxItem(false)]
  public class ColorItemControl : Control
  {
    private bool _isHovered;

    public bool IsChecked { get; set; }

    public Color HoverBorderColor { get; set; }

    public Color BorderColor { get; set; }

    protected override void OnMouseEnter(EventArgs e)
    {
      base.OnMouseEnter(e);
      _isHovered = true;
      Invalidate();
    }

    protected override void OnMouseLeave(EventArgs e)
    {
      base.OnMouseLeave(e);
      _isHovered = false;
      Invalidate();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
      base.OnPaint(e);

      Color color = _isHovered ? HoverBorderColor : BorderColor;

      using (var pen = new Pen(color))
      {
        e.Graphics.DrawRectangle(pen, new Rectangle(0, 0, Width - 1, Height - 1));
      }

      if (IsChecked)
        e.Graphics.DrawImageUnscaled(Resources.check, (Width - Resources.check.Width) / 2, (Height - Resources.check.Height) / 2);
    }
  }
}